package com.mgm.character.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgm.character.service.CharactersService;
import com.mgm.dao.CharactersMapper;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Characters;
import com.mgm.pojo.CharactersExample;
import com.mgm.pojo.CharactersExample.Criteria;
import com.mgm.pojo.User;
import com.mgm.result.IMessage;

/**
 * 
 * @ClassName: CharactersServiceImpl 
 * @Description: TODO(...) 
 * @author Andy Zhang
 * @date 2018年6月22日 下午1:22:10 
 *
 */
@Service
public class CharactersServiceImpl implements CharactersService{
	
	@Autowired
	private CharactersMapper charactersMapper;

	@Override
	public IMessage saveCharacters(Characters characters, User user) {
		characters.setCreateid(user.getId());
		characters.setCreatename(user.getName());
		characters.setCreatetime(new Date());
		
		charactersMapper.insert(characters);
		return IMessage.success();
	}

	@Override
	public IMessage updateCharacters(Characters characters, User user) {
		characters.setUpdateid(user.getId());
		characters.setUpdatename(user.getName());
		characters.setUpdatetime(new Date());
		
		charactersMapper.updateByPrimaryKey(characters);
		return IMessage.success();
	}

	@Override
	public IMessage deleteByKey(Integer id) {
		//TODO 判断权限是否可以删除 ing...
		charactersMapper.deleteByPrimaryKey(id);
		return IMessage.success();
	}

	@Override
	public DataGrid findAll(int pageNum, int pageSize) {
		//查询列表
		CharactersExample example = new CharactersExample();
		//分页处理
		PageHelper.startPage(pageNum, pageSize);
		List<Characters> list = charactersMapper.selectByExample(example);
		//创建一个返回值对象
		DataGrid dataGrid = new DataGrid();
		dataGrid.setItems(list);
		//获取PageInfo相关信息
		PageInfo<Characters> pageInfo = new PageInfo<>(list);
		dataGrid.setTotal_count(pageInfo.getTotal()); //总记录数
		dataGrid.setPage_count(pageInfo.getPages()); // 总页数
		dataGrid.setPage_index(pageInfo.getPageNum()); // 当前页
		return dataGrid;
	}
	
	@Override
	public List<Characters> findByName(String name){
		CharactersExample example = new CharactersExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		return charactersMapper.selectByExample(example);
	}

	@Override
	public Characters findByKey(Integer id) {
		return charactersMapper.selectByPrimaryKey(id);
	}

}
